// pages/transportConfirm/index.js
import requestUtil from '../../../baseRequest.js'
import requestUrls from '../../../requestUrl.js'
import { $Message } from '../../../components/base/index'
import commonUtils from '../../../utils/util.js'

let openid = wx.getStorageSync('openid')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid: openid,
    //driverInfo: {},
    uploadDriverLisence: {
      count: 1,
      uploadComplete: 'uploadDriverLisenceComplete',
      attentionText: '驾驶证仅需上传一张图片',
      uploadUrl: requestUrls.baseUrl + requestUrls.uploadUrl.driverLisence
    },
    isUploadDriverLisence: false,
    uploadDrivingLisence: {
      count: 1,
      uploadComplete: 'uploadDrivingLisenceComplete',
      attentionText: '行驶证仅需上传一张图片，非必须',
      uploadUrl: requestUrls.baseUrl + requestUrls.uploadUrl.drivingLisence
    },
    isUploadDrivingLisence: false,
    //是否提交过
    isSubmit: false
  },
  /**初始化获取救援司机信息 */
  getDriverInfo() {
    if (!this.data.openid) {
      this.setData({
        openid: wx.getStorageSync('openid')
      })
    }
    let param = {
      'openid': this.data.openid
    }
    requestUtil.postRequest(requestUrls.transportConfirm.init, param, (res) => {
      let info = res
      let drivingLicense = []
      if(info.drivingLicense){
        drivingLicense.push({...info.drivingLicense,baseUrl:requestUrls.baseImagePrefixUrl});
      }
      // info.drivingLicense ? drivingLicense.push(info.drivingLicense) : ''
      info.drivingLicense = drivingLicense
      let vehicleTravelLicense = []
      if(info.vehicleTravelLicense){
        vehicleTravelLicense.push({...info.vehicleTravelLicense,baseUrl:requestUrls.baseImagePrefixUrl})
      }
      // info.vehicleTravelLicense ? vehicleTravelLicense.push(info.vehicleTravelLicense) : ''
      info.vehicleTravelLicense = vehicleTravelLicense
     
      this.setData({
         driverInfo: info
      })
      console.log("-=--driverInfo",this.data.driverInfo)
    }, () => { })
     
  },
  
  /**保存第一步 承运方信息*/
  saveStepOne() {
    let param = this.data.formTemp
    param.openid = this.data.openid
    let url = requestUrls.transportConfirm.stepOne
    console.log('param', param, 'url', url)
    requestUtil.postRequest(url, param, (res) => {
      this.setData({
        showSpin: false
      })
      if (res.code == 10012) {
        this.setData({
          
          isSubmit: true
        })
        wx.showModal({
          title: '提交失败',
          content: res.message,
          showCancel: false,
          success: res => { }
        })
        return
      }
      //进入第二步，填写接货确认单
      wx.reLaunch({
        url: '../stepTwo/index?id='+res.id
      })
       
    }, () => { })
  },
   
  /**提交表单信息 */
  submitForm({ detail }) {
    console.log(detail.value)
    this.setData({
      ['formTemp.openid']:this.data.openid
    })
    
    if (this.checkFormData(detail.value)) {
      console.log('是否已经提交过', this.data.isSubmit)
      if (this.data.isSubmit) {
          this.setData({
            showSpin: true,
          })
          this.saveStepOne()
      }else{
          this.setData({
            showSpin: true,
            isUploadDriverLisence: true,
            formTemp: detail.value
          })
      }
        // 返回顶部
        wx.pageScrollTo({
          scrollTop: 0,
        })
        // wx.showLoading({
        //   title: '图片上传中'
        // })
    }
  },
  /**检查表单信息 */
  checkFormData(data) {
    // return false
    if (!data.mobile || data.mobile == '') {
      this.showErrorMessage('手机号不能为空')
      return false
    } else if(!this.data.openid){
      this.showErrorMessage('参数有误，请退出刷新')
      return false
    }else if (!data.carNumber || data.carNumber.length != 7) {
      this.showErrorMessage('请按要求填写认证车牌号码')
      return false
    } else if (!data.drivingLicense || data.drivingLicense.length != 1) {
      this.showErrorMessage('驾驶证上传图片数量有误')
      return false
    }
    return true
  },
  /**完成驾驶证上传回调*/
  uploadDriverLisenceComplete({ detail }) {
    console.log('完成驾驶证上传回调', detail)
    let status = detail.status
    if (status || status == 'error') {
      this.showErrorMessage('驾驶证上传失败，请重新选择上传');
        this.setData({
          showSpin: false,
          ['formTemp.drivingLicense']: "",
        })
      return
    }
    this.setData({
      ['formTemp.drivingLicense']: detail[0].url,
      isUploadDriverLisence: false,
      isUploadDrivingLisence: true
    })
    console.log('完成驾驶证上传回调结束，开始上传行驶证', detail)
  },
  /**完成行驶证上传回调*/
  uploadDrivingLisenceComplete({ detail }) {
    console.log('完成行驶证上传回调', detail)
    if(detail&&detail.length>0){
let status = detail.status
    if (status || status == 'error') {
        this.showErrorMessage("行驶证上传失败，请重新选择上传");
        this.setData({
          showSpin: false,
          ['formTemp.vehicleTravelLicense']: "",
          isUploadDrivingLisence: false,
        })
        return;
    }else{

      this.setData({
        ['formTemp.vehicleTravelLicense']: detail[0].url,
        ['formTemp.openid']: this.data.openid,
        isUploadDrivingLisence: false,
      })
    
    }
    }
    
    // console.log('formTemp', this.data.formTemp)
    wx.hideLoading()
    this.saveStepOne()
    
  },
  /**显示错误信息 */
  showErrorMessage(msg) {
    $Message({
      content: msg,
      type: 'error'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取司机初始化数据
    this.getDriverInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
   
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function() {

  // }
})
